using UnityEngine;
using System.Collections;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

public class SaveDataManager{
#region SINGLETON
private static SaveDataManager instance = null;
public static SaveDataManager GetInstance(){
	if (instance == null)
		instance = new SaveDataManager();
	return instance;
}
private SaveDataManager(){}
#endregion
	public void SaveFile(object obj, string fileName){
		IFormatter formatter = new BinaryFormatter();
		string name = "./saves/" + fileName + ".bin";
		System.IO.Directory.CreateDirectory("saves");
		Stream stream = new FileStream(name, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
		formatter.Serialize(stream, obj);
		stream.Close();
	}
	public Type LoadFile<Type>(string fileName){
		IFormatter formatter = new BinaryFormatter();
		string name = "./saves/" + fileName + ".bin";
		Stream stream = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.Read);
		Type obj = (Type) formatter.Deserialize(stream);
		stream.Close();
		return obj;
	}
   
}
/*	public void LoadFile(string fullpath)
	    {
        FileStream stream = File.Open(fullpath, FileMode.OpenOrCreate, FileAccess.Read);
		
        try
        {
            XmlSerializer serializer = new XmlSerializer(typeof(PlayerProfile));
            Globals.CurrentProfile = (PlayerProfile)serializer.Deserialize(stream);
        }
        catch (Exception ex)
        {
            Debug.Log(ex.Message);
        }

        finally
        {
            stream.Close();
        }
    }

    public void SaveFile(string fullpath)
    {
        File.Delete(fullpath);
        FileStream stream = File.Open(fullpath, FileMode.OpenOrCreate);
        try
        {
            XmlSerializer serializer = new XmlSerializer(typeof(PlayerProfile));
            serializer.Serialize(stream, Globals.CurrentProfile);
        }
        finally
        {
            stream.Close();
        }
    }
    */
